from bs4 import BeautifulSoup
import re
import urllib.request
import urllib.error
import random
import csv
from lxml import etree

def main():
    data = gateData()
    # saveDate(data)

findtitle = re.compile(r'<a class="li-hv" href=".*?" title="(.*?)">',re.S)
findimg = re.compile(r'<img class="lazy".*src="(.*?)".*>',re.S)
def gateData():
    i=1
    while i <5:
        url = 'http://ziziyy1.com/acg/2021/0/japan/{}.html'.format(i)
        i=i+1
        html = askURL(url)
        soup=BeautifulSoup(html,'html.parser')
        for wrap in soup.find_all('div',class_='wrap'):
            wrap=str(wrap)
            data=[]
            title=re.findall(findtitle,wrap)
            img=re.findall(findimg,wrap)
            data.append(title)
            data.append(img)
    return data



def askURL(url):
    headers = {
        'User-Agent':' Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 88.0.4324.146Safari / 537.36'
    }

    request = urllib.request.Request(url=url,headers=headers)
    html = ''
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode('utf-8')
        # print(html)
    except urllib.error.URLError as e:
        if hasattr(e,'code'):
            print(e.code)
        if hasattr(e,'reason'):
            print(e.reason)
    return html



main()